﻿using StudentManagement.IRepository;
using StudentManagement.IService;
using StudentManagement.Model.DTO;
using StudentManagement.Model.Entitys;
using StudentManagement.Model.Exceptions;
using System;
using System.Collections.Generic;
using System.Data;

namespace StudentManagement.Service
{
    public class StudentService : IStudentService
    {
        private readonly IStudentRepository _studentRepository;
        public StudentService(IStudentRepository studentRepository)
        {
            _studentRepository = studentRepository;
        }

        public void Delete(int id)
        {
            _studentRepository.Delete(id);
        }

        public StudentEntity GetById(int id)
        {
            return _studentRepository.GetById(id);
        }

        public DataTable GetData(string qs)
        {
            var data = _studentRepository.GetData(qs);

            if (data.Rows.Count < 1)
            {
                throw new BusinessExeption("无数据可导出");
            }

            return data;
        }

        public void Insert(StudentEntity entity)
        {
            if (_studentRepository.NumberIsExist(entity.Number))
            {
                throw new BusinessExeption($"【{entity.Number}】学号已经绑定了其他学生");
            }
            _studentRepository.Insert(entity);
        }

        public Tuple<List<StudentQueryDTO>, int> Search(string qs, int limit, int page)
        {
            return _studentRepository.Search(qs, limit, page);
        }

        public void Update(StudentEntity entity)
        {
            if (_studentRepository.NumberIsExist(entity.Number, entity.Id))
            {
                throw new BusinessExeption($"【{entity.Number}】学号已经绑定了其他学生");
            }
            _studentRepository.Update(entity);
        }
    }
}
